package com.rt.code.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.code.dto.A2userDto;
import com.rt.code.dto.PageDto;
import com.rt.code.mapper.A2userMapper;
import com.rt.code.service.intf.IA2userService;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.code.utils.page.MyPageUtils;
import com.rt.tmpt.dto.Result;
import com.rt.tmpt.service.impl.DataLimitService;
import com.rt.tmpt.service.impl.SecurityV2Service;
import com.rt.tmpt.utils.DateTool;
import com.rt.code.bean.A2user;
import com.rt.tmpt.utils.StringUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by wxy on 2016/7/28.
 */
@Service(value="a2userService")
@Transactional(propagation = Propagation.REQUIRED)
public class A2userService extends ServiceImpl<A2userMapper, A2user> implements IA2userService {


    @Autowired
    private DataLimitService dataLimitService;
    @Autowired
    private MyMapper mapper;
    @Autowired
    private MyPageUtils myPageUtils;
    @Resource
    private A2userMapper a2userMapper;
    @Resource
    private SecurityV2Service securityV2Service;
/*
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
*/

    @Autowired
    PasswordEncoder passwordEncode;

    /**
     * 数据报表统计
     * @param a2userDto
     * @param pageDto
     * @param paramMap
     * @return
     */
    @Override
    public Result<A2userDto> pagination4Sum(A2userDto a2userDto, PageDto pageDto, Map<String,Object> paramMap) {
        //数据边界控制参数
        Map<String,Object> paramMap1 = dataLimitService.getLimitParam(paramMap);

        //分页查询
        Result<A2userDto> result = myPageUtils.pagination(
                a2userDto,pageDto,
                A2user.class,
                A2userDto.class,
                a2userMapper,
                paramMap1,
                false,null);


        return result;
    }



    @Override
    public Result<A2userDto> pagination(A2userDto a2userDto, PageDto pageDto) {
        //分页查询
        Result<A2userDto> result = myPageUtils.pagination(
                a2userDto,pageDto,
                A2user.class,
                A2userDto.class,
                a2userMapper,
                false,null);

        return result;
    }

    /**
    * 分布查询，有数据边界控制功能
    * @param a2userDto
    * @param pageDto
    * @param paramMap
    * @return
    */
    @Override
    public Result<A2userDto> pagination(A2userDto a2userDto, PageDto pageDto, Map<String,Object> paramMap) {
        //数据边界控制参数
        Map<String,Object> paramMap1 = dataLimitService.getLimitParam(paramMap);

        //分页查询
        Result<A2userDto> result = myPageUtils.pagination(
                a2userDto,pageDto,
                A2user.class,
                A2userDto.class,
                a2userMapper,
                paramMap1,
                false,null);

        return result;
    }

    @Override
    public Result<A2userDto> selectAll() {
        Result<A2userDto> result = new Result();

        //排序
        PageDto pageDto = new PageDto();
        pageDto.setSortColumn("id");
        pageDto.setSortOrder("desc");
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("page",pageDto);

        List<A2user> list = a2userMapper.mySelectAll(mapParam);
        List<A2userDto> dtoList = mapper.maplist(list, A2userDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<A2userDto> selectById(long id) {
        Result<A2userDto> result = new Result();

        A2user a2user = a2userMapper.mySelectById(id);
        A2userDto dto = mapper.map(a2user, A2userDto.class);
        result.setObj(dto);

        return result.success();
    }


    /**
        返回ID值
    */
    @Override
    public Result<A2userDto> addData(A2userDto a2userDto){
        Result<A2userDto> result = new Result();

        //封闭数据
        A2user a2user = mapper.map(a2userDto, A2user.class);
        a2user.setCreatedate(DateTool.getCurTimestamp());

        if(a2user.getSex() == null){
            a2user.setSex(0);
        }

        //密码加密
        a2user.setPassword(passwordEncode.encode(a2user.getPassword()));


        //验证loginname是否存在
        int countByLoginname = a2userMapper.mySelectListCountByLoginname(a2user.getLoginname());
        if(countByLoginname>0){
            return result.fault("登录名已存在");
        }
        //调用mapper
        long i = a2userMapper.myInsert(a2user);
        if(i==0){
            return result.fault("插入失败");
        }

        A2userDto dto = mapper.map(a2user, A2userDto.class);
        result.setObj(dto);

        //insert role
        String roleIds = a2userDto.getRoleIds();
        if(!StringUtils.isNull(roleIds)) {
            List<String> roleIdStrList = Arrays.asList(roleIds.split(","));
            List<Long> roleIdList = new ArrayList<>();
            for(String roleId: roleIdStrList){
                roleIdList.add(Long.parseLong(roleId));
            }
            Result result1 = securityV2Service.bindUser_Role(a2user.getId(), roleIdList, null);
            if(!result1.isSuccess()){
                throw new RuntimeException("更新角色失败");
            }
        }

        return result.success();
    }

    /**
        返回ID值
    */
    @Override
    public Result<A2userDto> editData(A2userDto a2userDto){
        Result<A2userDto> result = new Result();

        //封装数据
        A2user a2user = mapper.map(a2userDto, A2user.class);
        a2user.setModifydate(DateTool.getCurTimestamp());

        //调用mapper
        long i = a2userMapper.myUpdateById(a2user);
        if(i==0){
            return result.fault("编辑数据失败");
        }

        //密码加密, 重置密码
        if(!StringUtils.isNull(a2user.getPassword())) {
            a2user.setPassword(passwordEncode.encode(a2user.getPassword()));
            a2userMapper.myUpdatePasswordById(a2user);
        }

        A2userDto dto = mapper.map(a2user, A2userDto.class);
        result.setObj(dto);

        //insert role
        String roleIds = a2userDto.getRoleIds();
        if(!StringUtils.isNull(roleIds)) {
            List<String> roleIdStrList = Arrays.asList(roleIds.split(","));
            List<Long> roleIdList = new ArrayList<>();
            for(String roleId: roleIdStrList){
                roleIdList.add(Long.parseLong(roleId));
            }
            Result result1 = securityV2Service.bindUser_Role(a2user.getId(), roleIdList, null);
            if(!result1.isSuccess()){
                throw new RuntimeException("更新角色失败");
            }
        }

        return result.success();
    }

    /**
    伪删除记录，返回ID值
    */
    @Override
    public Result deleteById(long id, String adminMan){
        Result result = new Result();

        Map<String,Object> mapParameter = new HashMap();
        mapParameter.put("id",id);
        mapParameter.put("modifyman",adminMan);
        mapParameter.put("modifydate", DateTool.getCurTimestamp());

        //调用mapper
        long i = a2userMapper.myDeleteById(mapParameter);
        if(i==0){
            return result.fault("删除数据失败");
        }

        return result.success();
    }





}
